﻿#ifndef _EASY_EPOLL_SERVER_H_
#define _EASY_EPOLL_SERVER_H_

#if __linux__

#include "EasyTcpServer.hpp"
#include "CELLEpoll.hpp"
#include "CELLLog.hpp"
#include "CELLEpollServer.hpp"

class EasyEpollServer : public EasyTcpServer
{
public:
	~EasyEpollServer() {}

	void Start(int nCELLServer)
	{
		EasyTcpServer::Start<CELLEpollServer>(nCELLServer);
	}
private:
	// 处理网络消息
	virtual void OnRun(CELLThread* pthread) override
	{
		CELLEpoll ep;
		ep.create(1);
		ep.ctl(EPOLL_CTL_ADD, sockfd(), EPOLLIN);
		while (pthread->isRun())
		{
			time4msg();

			int ret = ep.wait(1);
			if (ret < 0)
			{
				CELLLog_pError("EasyEpollServer::OnRun error");
				pthread->Exit();
				break;
			}
			auto events = ep.events();
			for (int i = 0; i < ret; ++i)
			{
				if (events[i].data.fd == sockfd())
				{
					if (events[i].events & EPOLLIN)
					{
						Accept();
					}
				}
			}
		}
		CELLLog_Info("EasyEpollServer OnRun exit");
	}
};

#endif // #if __linux__

#endif // #ifndef _EASY_EPOLL_SERVER_H_
